package com.netty.cn.rpc.protoc.server;

import com.netty.cn.rpc.protoc.model.ClientModel;
import com.netty.cn.rpc.protoc.model.ServerModel.ServerDataModel;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 
 * @author wangzonghui
 * @date 2021-07-19 17:40:34
 * @Description protoc 服务端handler
 */
public class ProtocServerHandler extends SimpleChannelInboundHandler<ClientModel.ClientDataModel>{
	
    
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{
    	super.exceptionCaught(ctx,cause);
        System.out.println(ctx.channel().remoteAddress()+" 关闭连接");
    }

    /**
     * 数据接收
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx,ClientModel.ClientDataModel clientDataModel) throws Exception {
    	System.out.println("Server 接收数据："+clientDataModel.getChildInfo().getContext());

    	ServerDataModel.Builder serverDataModel=ServerDataModel.newBuilder();
    	serverDataModel.setMsg("服务端响应消息");
    	
    	//序列化
        ctx.writeAndFlush(serverDataModel.build());
        
    }
}